﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using ACLCTaguigEnrollmentSystem.Forms;

namespace ACLCTaguigEnrollmentSystem.UserControls
{
    public partial class uc_AdmissionStudentRecords : UserControl
    {

        SqlConnection conn = new SqlConnection(Classes.ConnectionString.connString);

        public uc_AdmissionStudentRecords()
        {
            InitializeComponent();
        }

        private void uc_AdmissionStudentRecords_Load(object sender, EventArgs e)
        {
            DataGridViewStudent();
        }

        #region DataPopulation
        private void DataGridViewStudent()
        {
            SqlCommand cmd = new SqlCommand("SELECT UniversalStudentNo,	a.StudentNo, LastName + ', ' +FirstName + ' ' + MiddleName as Name,	f.DegreeCode + ' '+c.CourseName as CourseName, CAST(e.SchoolYearFrom as varchar(30)) + ' - ' + CAST(e.SchoolYearTo as varchar(30))  as SchoolYear,d.YearName FROM Students a LEFT JOIN Enrollment b ON a.StudentID = b.StudentID  LEFT JOIN Courses c ON b.CourseID = c.CourseID   LEFT JOIN Year d ON b.YearID = d.YearID  LEFT JOIN SchoolYear e ON b.SchoolYearID = e.SchoolYearID  LEFT JOIN Degree f ON c.DegreeId = f.DegreeID ", conn);
            //cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);

            dgv_Student.DataSource = dt;
        }
        #endregion

        private void dgv_Student_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            if (dgv == null)
                return;
            if (dgv.CurrentRow.Selected)
            {
                if (e.RowIndex >= 0)
                {
                    DataGridViewRow row = this.dgv_Student.Rows[e.RowIndex];
                    Classes.Variable.StudentNo = row.Cells["StudentNo"].Value.ToString();
                    //txt_StudentNo.Text = Classes.Variable.StudentId;
                    SqlCommand cmd = new SqlCommand("dbo.spStudent_Select", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@StudentNo", Classes.Variable.StudentNo);
                    //cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    conn.Open();
                    lbl_Name.Text = dt.Rows[0].ItemArray[3].ToString() + ",  " + dt.Rows[0].ItemArray[4].ToString() + " " + dt.Rows[0].ItemArray[5].ToString();
                    lbl_UniversalStudentNo.Text = dt.Rows[0].ItemArray[1].ToString();
                    lbl_StudentNumber.Text = dt.Rows[0].ItemArray[2].ToString();
                    lbl_Course.Text = dt.Rows[0].ItemArray[6].ToString();
                    lbl_Year.Text = dt.Rows[0].ItemArray[7].ToString();
                    txt_Sex.Text = dt.Rows[0].ItemArray[8].ToString();
                    txt_CivilStatus.Text = dt.Rows[0].ItemArray[9].ToString();
                    txt_PresentAddress.Text = dt.Rows[0].ItemArray[10].ToString();
                    txt_Telephone.Text = dt.Rows[0].ItemArray[11].ToString();
                    txt_Mobile.Text = dt.Rows[0].ItemArray[12].ToString();
                    txt_EmailAddress.Text = dt.Rows[0].ItemArray[13].ToString();
                    txt_Birthdate.Text = dt.Rows[0].ItemArray[14].ToString();
                    txt_Citizenship.Text = dt.Rows[0].ItemArray[15].ToString();
                    txt_Religion.Text = dt.Rows[0].ItemArray[16].ToString();
                    txt_LastSchoolAttended.Text = dt.Rows[0].ItemArray[17].ToString();
                    txt_FathersName.Text = dt.Rows[0].ItemArray[18].ToString();
                    txt_MothersName.Text = dt.Rows[0].ItemArray[19].ToString();
                    txt_DateEnrolled.Text = dt.Rows[0].ItemArray[20].ToString();
                    txt_SchoolYear.Text = dt.Rows[0].ItemArray[21].ToString();

                    conn.Close();
                }
            }
        }

        private void tsb_StudentAdd_Click(object sender, EventArgs e) 
        {
 
        }
    }
}